"use strict";

(function() {
	alx.svg.Rect = function (svgArgs) {
		var me = this,		
				x, 
				y, 
				width,
				height,
				disposed = false,
				svgRect = null,
				points  = "",
				origPos = null,
				visibility = null,
				secPos  = null;
				
				
		var init = function() {
			svgRect = document.createElementNS(alx.svg.ns, "rect");
			
			visibility = svgArgs.visibility || "visible";
			me.setAttributes(svgArgs);
		};

		me.isVisible = function () {
			return visibility;
		};
		
		me.setAttributes = function (svgAttr) {
			svgRect && svgAttr && alx.svg.setAttributeMap(svgAttr, svgRect);
		};
		
		me.isAttached = function () {
			if (svgRect && svgRect.parentNode) 
				return true;
			
			return false;
		};
		
		me.getElement = function () {
			return svgRect;
		};
		
		me.intersect = function (rect) {
			var tb = me.getBounds(), // This rect's bounds
					sb = null,           // Second rect's bounds
					ir = {},             // Intersecting rect's bounds
					area = 0;
			
			if (rect instanceof alx.svg.Rect) {
				sb = rect.getBounds();
			} else {
				sb = rect;
			}
			
			if ((sb.left >= tb.left) && (sb.left <= tb.right)) {
				ir.left = sb.left;
			}
			else if ((tb.left >= sb.left) && (tb.left <= sb.right)) {
				ir.left = tb.left;
			}
			
			if ((sb.right >= tb.left) && (sb.right <= tb.right)) {
				ir.right = sb.right;
			}
			else if ((tb.right >= sb.left) && (tb.right <= sb.right)) {
				ir.right = tb.right;
			}
			
			if ((sb.top >= tb.top) && (sb.top <= tb.bottom)) {
				ir.top = sb.top;
			}
			else if ((tb.top >= sb.top) && (tb.top <= sb.bottom)) {
				ir.top = tb.top;
			}
			
			if ((sb.bottom >= tb.top) && (sb.bottom <= tb.bottom)) {
				ir.bottom = sb.bottom;
			}
			else if ((tb.bottom >= sb.top) && (tb.bottom <= sb.bottom)) {
				ir.bottom = tb.bottom;
			}
			
			if (ir.top && ir.right && ir.bottom && ir.left) {
				return ir;
			}
			else {
				return null;
			}
		};
		
		me.hide = function () {
			if (visibility != "hidden") {
				visibility = "hidden";
				svgRect && svgRect.setAttribute("visibility", visibility);
			}
		};
		
		me.show = function () {
			if (visibility != "visible") {
				visibility = "visible";
				svgRect && svgRect.setAttribute("visibility", visibility);
			}
		};
		
		me.setOrigin = function (pos) {
			origPos = pos;
			secPos && refreshAttributes();	
		};
		
		me.setSecond = function (pos) {
			secPos = pos;
			origPos && refreshAttributes();
		};		
	
		me.dispose = function () {
			svgRect && svgRect.parentNode && svgRect.parentNode.removeChild(svgRect);
			svgRect = null;
			disposed = true;
		};
		
		me.getBounds = function () {
			return {
				top:    y,
				left:   x,
				bottom: y+height,
				right:  x+width
			};
		};
		
		var refreshAttributes = function() {
			if (secPos.x > origPos.x) {
				x = origPos.x;
				width = secPos.x - origPos.x;
			} else {
				x = secPos.x;
				width = origPos.x - secPos.x;
			}
			
			if (secPos.y > origPos.y) {
				y = origPos.y;
				height = secPos.y - origPos.y;
			} else {
				y = secPos.y;
				height = origPos.y - secPos.y;
			}
			
			alx.svg.setAttributeMap({
				"x":      x,
				"y":      y,
				"width":  width,
				"height": height
			}, svgRect);
		};
		
		
		// ------------------------------------------------------------- //
		init();
	};
})();